﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class BirdMoveController : MonoBehaviour {

	[SerializeField] private UnityEvent birdLostEvent;
	[SerializeField] private LayerMask obstacleLayer;
	[SerializeField] private float flySpeed = 5;
	//public bool Controllable {
	//	get;
	//	set;
	//}

	[SerializeField] private PlayerMarker m_Marker;

	public BirdState.State state {
		get;
		set;
	}

	BirdAnimController anim;
	Rigidbody2D rb;
	CharacterController cc;

	public Transform idlePos;
	public Transform boyPos;

	Vector2 movement;

	bool m_CallOver = true;

	private void Awake() {
		state = BirdState.State.NonControllable;
		anim = gameObject.GetComponent<BirdAnimController>();
		cc = gameObject.GetComponent<CharacterController>();
	}

	private void Update() {
		if (BirdState.Controllable(state)) {
			movement.x = Input.GetAxisRaw("Horizontal");
			movement.y = Input.GetAxisRaw("Vertical");
			if (!movement.Equals(Vector2.zero))
				m_CallOver = true;
			if (!m_CallOver)
				moveToPos(idlePos.position);
			else
				Move();
		} else if (BirdState.Wait(state)) {
			WatiForBoy();
		}

	}

	public void Move() {

		if (Input.GetKeyDown(KeyCode.J)) {
			anim.PlayState(BirdAnimController.State.FlyAction);
		}


		if((movement.y > 0) || anim.MState == BirdAnimController.State.Fly) {
			anim.PlayState(BirdAnimController.State.Fly);
			rb.gravityScale = .3f;
		} else if(movement.x != 0 && movement.y == 0) {
			anim.PlayState(BirdAnimController.State.Walk);
		} else if(movement.x == 0 && anim.MState == BirdAnimController.State.Walk)
			anim.PlayState(BirdAnimController.State.Idle);

		if (!cc.Grounded) {
			anim.PlayState(BirdAnimController.State.Fly);
			rb.gravityScale = .3f;
		}

		cc.Flip(movement.x);

		rb.velocity = movement* flySpeed;

	}

	public void WatiForBoy() {
		rb.gravityScale = 0;
		rb.velocity = Vector2.zero;

		//小男孩与鸟的直线距离没有障碍物
		RaycastHit2D rh2 = Physics2D.Raycast(transform.position + (boyPos.position - transform.position).normalized, (boyPos.position - transform.position).normalized, Mathf.Infinity, obstacleLayer);

		if (rh2 && rh2.transform.tag == TagAndLayersConfigs.Tag.Player) {
			state = BirdState.State.NonControllable;
		}

	}

	public void CallByBoy(bool controllable) {
		StartCoroutine(showMarker(controllable));

		if (controllable && BirdState.Wait(state)) {
			birdLostEvent.Invoke();
			return;
		}
		
		//state = controllable ? BirdState.State.Controllable : BirdState.State.Wait;
		state = controllable ? BirdState.State.Controllable : BirdState.State.NonControllable;
	
		if(rb == null)
			rb = gameObject.GetComponent<Rigidbody2D>();

		rb.gravityScale = 0;

		m_CallOver = false;
	}

	IEnumerator showMarker(bool controllable) {

		m_Marker.ShowMarker(true, controllable ? 1 : 0);

		yield return new WaitForSeconds(3);
		m_Marker.ShowMarker(false, controllable ? 1 : 0);

	}


	void moveToPos(Vector3 pos) {

		if (Vector2.Distance(transform.position, pos) < .2f)
			m_CallOver = true;

		if (!m_CallOver) {
			anim.setMove(2);
			rb.velocity = pos - transform.position;
		}

	}
	public void OnFalling(bool notGrounded) {
		if (!notGrounded && BirdState.Controllable(state)) {
			anim.setMove(0);
			rb.gravityScale = 3;
		}
	}

	private void OnTriggerStay2D(Collider2D collision) {
		if (BirdState.Controllable(state) && InputReader.InterActive && collision.CompareTag(TagAndLayersConfigs.Tag.IntertactiveItems)) {
			collision.GetComponent<InteractiveItems>().Interactive_Interface(true);
		}
	}

}
